package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.InspectionItemsService;
import com.itheima.service.InspectionUnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/checkgroup")
public class InspectionUnitController {

    @Autowired
    private InspectionUnitService inspectionUnitService;


    /**
     * 查询所有检查组
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result getAllInspectionUnit() {
        List<CheckGroup> list = inspectionUnitService.getAllInspectionUnit();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }

    /**
     * 分页查询检查组
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult getInspectionUnitPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询检查组");
        return inspectionUnitService.findPage(queryPageBean);
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        CheckGroup checkGroup = inspectionUnitService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }


    /**
     * 新增检查组
     *
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result addInspectionUnit(@RequestParam("checkitemIds") List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup) {
        log.info("新增检查组checkGroup:{}", checkGroup);
        log.info("新增检查组ids:{}", checkitemIds);
        inspectionUnitService.addInspectionUnit(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 修改检查组
     *
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/edit")
    public Result editInspectionUnit(@RequestParam("checkitemIds") List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup) {
        log.info("修改检查组checkGroup:{}", checkGroup);
        log.info("修改检查组ids:{}", checkitemIds);
        inspectionUnitService.editInspectionUnit(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    /**
     * 根据id删除检查组
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteInspectionUnitById(@RequestParam("id") Integer id) {
        log.info("删除检查组");
        inspectionUnitService.deleteInspectionUnit(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }


    /**
     * 根据套餐id查询关联的检查项id
     *
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam("setmealId") Integer setmealId) {
        List<Integer> list = inspectionUnitService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }



}
